package com.bawei.module_my.view.activity

import android.content.Intent
import android.graphics.BitmapFactory
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.module_base.net.util.MMKVUtil
import com.bawei.module_base.ui.base.BaseActivity
import com.bawei.module_my.R
import com.bawei.module_my.databinding.ActivityReplaceProfilePictureBinding
import com.bawei.module_my.viewmodel.MyViewModel
import com.blankj.utilcode.util.UriUtils
import com.bumptech.glide.Glide

import java.io.File

@Route(path = "/my/replace_profile_picture_activity")
class ReplaceProfilePictureActivity : BaseActivity<MyViewModel, ActivityReplaceProfilePictureBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_replace_profile_picture
    }
    private lateinit var file: File
    override fun initData() {

          Glide.with(this)
              .load(MMKVUtil.getHeadPic())
              .into(binding.imageReplace)


        binding.pictureReplace.setOnClickListener(View.OnClickListener {

            //上传图片
            showPopupWindow()
        })
        viewModel.dataModifyHeadPic.observe(this){
            MMKVUtil.setPic(it.result.toString())
            if (it.status=="0000"){
                Log.i("TAGnnnn", "showPopupWindow: "+it.message)
                finish()
            }
        }
    }
    fun showPopupWindow(){
        //加载布局
        val view = LayoutInflater.from(this).inflate(R.layout.dialog_up_picture_layout, null)
        //获取PopupWindow对象 设置宽度为match为 高度自适应
        val popWnd: PopupWindow = PopupWindow(view,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,true)
        popWnd.setContentView(view)
        val sum = view.findViewById<ConstraintLayout>(R.id.sum)
        //设置位置
        popWnd.showAtLocation(sum, Gravity.BOTTOM, 0,0)
        popWnd.isFocusable=false
        //拍照
        val photograph = view.findViewById<TextView>(R.id.photograph)
        photograph.setOnClickListener(View.OnClickListener {
            popWnd.dismiss()
            Toast.makeText(this,"暂不支持拍照功能",Toast.LENGTH_SHORT)
        })
        //去相册
        val album_replace = view.findViewById<TextView>(R.id.album_replace)
        album_replace.setOnClickListener(View.OnClickListener {
//            val intent = Intent(Intent.ACTION_GET_CONTENT)
                val intent = Intent(Intent.ACTION_PICK)
                intent.type="image/*"
                startActivityForResult(intent,0)
        })
        //取消
        val delete = view.findViewById<TextView>(R.id.delete_replace)
        delete.setOnClickListener(View.OnClickListener {
            //取消
            popWnd.dismiss()
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==0 && data!=null){
            file = UriUtils.uri2File(data.data)
            binding.imageReplace.setImageBitmap(BitmapFactory.decodeFile(UriUtils.uri2File(data.getData()).absolutePath))
            viewModel.postModifyHeadPic(file)

        }
    }
    override fun getViewModelClass(): Class<MyViewModel> {
       return MyViewModel::class.java
    }

}